package com.example.ibeacon_station;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class BeaconListAdapter extends RecyclerView.Adapter<BeaconListAdapter.BeaconViewHolder> {
    private List<Beacon> beaconList = new ArrayList<>();

    public void setBeaconList(List<Beacon> beaconList) {
        this.beaconList = beaconList;
    }

    @NonNull
    @Override
    public BeaconViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.beacon_item_simple,parent,false);
        return new BeaconViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull BeaconViewHolder holder, int position) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Beacon beacon = beaconList.get(position);
        holder.textViewTitle.setText(beacon.getmTitle());
        holder.textViewInfo.setText(beacon.getmMacAddress());
        holder.textViewRSSI.setText(String.valueOf(beacon.getRSSI()));
        holder.textViewTime.setText(sdf.format(beacon.getmTimeStamp()));
//        holder.textViewMajor.setText(String.valueOf(beacon.getmMajor()));
//        holder.textViewMinor.setText(String.valueOf(beacon.getmMinor()));
//        holder.textViewRSSI2.setText(String.valueOf(beacon.getRSSI()));
//        holder.textViewTxPower.setText(String.valueOf(beacon.getmTxPower()));
    }


    @Override
    public int getItemCount() {
        return beaconList.size();
    }

    static class BeaconViewHolder extends RecyclerView.ViewHolder{
        TextView  textViewTitle, textViewInfo, textViewRSSI, textViewTime,
                textViewMajor, textViewMinor, textViewRSSI2,textViewTxPower;
        public BeaconViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.beaconTitle);
            textViewInfo = itemView.findViewById(R.id.beaconMac);
            textViewRSSI = itemView.findViewById(R.id.beaconRSSI);
            textViewTime = itemView.findViewById(R.id.beaconTime);
//            textViewMajor = itemView.findViewById(R.id.beaconMajor);
//            textViewMinor = itemView.findViewById(R.id.beaconMinor);
//            textViewRSSI2 = itemView.findViewById(R.id.beaconRSSI2);
//            textViewTxPower = itemView.findViewById(R.id.beaconTxPower);
        }
    }
}
